<?php

/**
 * nalog actions.
 *
 * @package    marketing
 * @subpackage nalog
 * @author     Your name here
 */
class nalogActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $date= getdate();
	$default=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        
        $date=getdate(mktime(0,0,0, $date["mon"]-1, $date["mday"], $date["year"]));
        $default1=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        $filter = array(
            "od" => $default1,
            "do" => $default,
	    "status" => '',
        );
        if ($request->hasParameter('id')) {
            $c = new Criteria();
            $c->add(NarudzbinaPeer::ID, $request->getParameter('id'));
        } else if ($request->hasParameter('nalog_id')) {
            $c = new Criteria();
            $c->add(NalogProdajePeer::ID, $request->getParameter('nalog_id'));
        } else {
            if ($request->hasParameter('narudzbina')) {


                $this->getUser()->setAttribute('nalog', $request->getParameter('narudzbina'));
                $this->redirect('nalog/index');
            }
            if ($this->getUser()->hasAttribute('nalog')) {
                $filter = $this->getUser()->getAttribute('nalog');
            }
            $c1 = null;
            $c2 = null;
            $c = new Criteria();
            if ($filter['od'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['od']['month'], $filter['od']['day'], $filter['od']['year']));
                $c1 = $c->getNewCriterion(NalogProdajePeer::DATUM_REALIZACIJE, $from, Criteria::GREATER_EQUAL);
            }
            if ($filter['do'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['do']['month'], $filter['do']['day'], $filter['do']['year']));
                $c2 = $c->getNewCriterion(NalogProdajePeer::DATUM_REALIZACIJE, $from, Criteria::LESS_EQUAL);
            }

            if ($c1 && $c2) {
                $c1->addAnd($c2);
                $c->add($c1);
            } else if ($c1)
                $c->add($c1);
            else if ($c2)
                $c->add($c2);
            if (!is_null($filter['status']) && $filter['status'] != null) {
                 $c->add(NarudzbinaPeer::STATUS_ID, $filter['status']);
            }
        }
        $c->addJoin(NalogProdajePeer::NARUDZBINA_ID, NarudzbinaPeer::ID, Criteria::INNER_JOIN);
               
        $this->filter = new NarudzbinaFilterForm();
        $this->filter->setDefaults($filter);
        $this->pager = new sfPropelPager('NalogProdaje', 3);
        $this->pager->setMaxPerPage(3);
        $this->pager->setCriteria($c);
        
        $this->pager->setPage($request->hasParameter('page') ? $request->getParameter('page') : 1);
        $this->pager->init();
        $this->nalog_prodaje_list = $this->pager->getResults();
    }

    public function executeCreate(sfWebRequest $request) {


        $nalog = new NalogProdaje();

        $this->setTemplate(null);

        $nalog->setOdgovornoLice($this->getUser()->getId());
        $nalog->setDatumRealizacije(getdate('Y-m_d H:i:s', mktime()));
        $nalog->setNarudzbinaId($request->getParameter('id'));
        $narudzbina = $nalog->getNarudzbina();
        $narudzbina->setStatusId(4);
        $narudzbina->save();
        $nalog->save();
        $this->redirect('nalog/index?id=' . $nalog->getNarudzbinaId());
    }

}
